package com.onehash.onebilling.entity;


import java.io.*;
import java.text.ParseException;
import java.util.*;

public class OneBillingStorage {
    // Instance of the singleton class
    private static OneBillingStorage storage;
    //////////////////////////////////////////////
    public static final String MAIN_OBJ_FILENAME="vrentObj.dat";
    public static final String ACC_OBJ_FILENAME="accObj.dat";
    public static final String GIFT_OBJ_FILENAME="gift.dat";
    public static final String DISCTYPE_FILENAME="discType.dat";
    
    public static final String USERS_OBJ_FILENAME="data\\user.dat";
    //public static final String TXNS_OBJ_FILENAME="data\\transaction.csv";
    //public static final String PAYMENTS_OBJ_FILENAME="data\\payment.csv";

    private OneBillingStorage(){

    }

    public static OneBillingStorage getInstance(){
        if(storage==null){
            storage=new OneBillingStorage();
        }
        return storage;
    }
    
    public void writeUsersObjects(HashMap <String, User> users, String filename) throws FileNotFoundException, IOException{
        FileOutputStream fo = new FileOutputStream(filename);
        ObjectOutputStream so = new ObjectOutputStream(fo);
        // Write all items class
        
        so.writeObject(users);
        so.flush();
    
        so.close();
        fo.close();
    }

    public HashMap <String, User> readUsersObjects(String filename) throws FileNotFoundException, IOException{
        FileInputStream fi = new FileInputStream(filename);
        ObjectInputStream si = new ObjectInputStream(fi);
       
        HashMap <String, User> users = new HashMap<String, User>();
        Object obj;
        try {
            if ((obj = si.readObject()) != null) {
                users = (HashMap <String, User>)obj;
            }
        } catch(EOFException eof) {
            //EOF reached
        }catch(ClassNotFoundException ce){
            // Class not found
        }

        return users;
    }


    
    

    public void writeObjects(Vector items, String filename) throws FileNotFoundException, IOException{
        FileOutputStream fo = new FileOutputStream(filename);
        ObjectOutputStream so = new ObjectOutputStream(fo);
        // Write all items class
        for (int i = 0; i < items.size(); i++) {
            so.writeObject(items.elementAt(i));
            so.flush();
        }
        so.close();
        fo.close();
    }

    public Vector readObjects(String filename) throws FileNotFoundException, IOException{
        FileInputStream fi = new FileInputStream(filename);
        ObjectInputStream si = new ObjectInputStream(fi);
        Vector list = new Vector();
        Object obj;
        try {
            while ((obj = si.readObject()) != null) {
                list.addElement(obj);
            }
        } catch(EOFException eof) {
            //EOF reached
        }catch(ClassNotFoundException ce){
            // Class not found
        }

        return list;
    }

}
